package com.library.controller;

import com.alibaba.fastjson.JSON;
import com.library.entity.Books;
import com.library.entity.User;
import com.library.entity.UserAction;
import com.library.service.BooksService;
import com.library.service.UserActionService;
import com.library.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.util.*;

/**
 * Created by Administrator on 2017/6/11.
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private HttpSession session;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private BooksService booksService;
    @Autowired
    private UserActionService userActionService;


    private static final Logger LOGGER = Logger.getLogger(UserController.class);

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(String studentId, String password){

        User user = null;
        String result = null;

        try {
            LOGGER.info("--------------"+studentId);
            user = userService.login(studentId,password);

        }catch (NullPointerException e){
            e.printStackTrace();
        }

        if (user == null){
            result = "false";
        }

        if (user != null){
            result = "true";
            session.setAttribute("USER",user);
        }
        return result;
    }

    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public void logout(){

        session.invalidate();

    }

    @RequestMapping(value = "/search",produces = "text/html;charset=UTF-8")
    public String search(String bookName){

        List<Books> booksList = null;
        if (bookName != null){

            booksList = booksService.search(bookName);

        }
        if (booksList == null){
            return "false";
        }
        return JSON.toJSON(booksList).toString();
    }

    @RequestMapping(value = "/filter",produces = "text/html;charset=UTF-8")
    public String filter(String type){

        List<Books> booksList = null;
        if (type != null){
            booksList = booksService.filter(type);
        }

        if (booksList == null){
            return "false";
        }
        return JSON.toJSON(booksList).toString();
    }

    @RequestMapping(value = "/allBooks",produces = "text/html;charset=UTF-8")
    public String allBooks(String Offset){

        int pageOffset = Integer.parseInt(Offset);
        List<Books> booksList = booksService.allBooks(pageOffset);

        return JSON.toJSON(booksList).toString();
    }

    @RequestMapping(value = "/pageSize",produces = "text/html;charset=UTF-8")
    public String pageSize(){

        List<Books> booksList = booksService.pageSize();
        int pageSize = booksList.size();
        return JSON.toJSON(pageSize).toString();
    }

    @RequestMapping(value = "/borrow",method = RequestMethod.GET)
    public String borrow(int id){

        Books books = booksService.findById(id);

        if (session.getAttribute("USER") != null){
            User user = (User) session.getAttribute("USER");

            Date now = new Date();
            DateFormat dateFormat = DateFormat.getDateInstance();
            String borrowTime = dateFormat.format(now);

            Calendar c = Calendar.getInstance();
            c.add(Calendar.DAY_OF_MONTH, 30);
            String returnTime = dateFormat.format(c.getTime());

            UserAction userAction = new UserAction();
            userAction.setUserid(user.getId());
            userAction.setBookid(books.getId());
            userAction.setBorrowtime(borrowTime);
            userAction.setReturntime(returnTime);
            userActionService.addUserAction(userAction);
            books.setName(books.getName());
            books.setNumber(books.getNumber()-1);
            books.setType(books.getType());
            books.setStatus(books.getStatus());
            books.setPublishedtime(books.getPublishedtime());
            books.setId(books.getId());
            booksService.updateBooks(books);
            return "true";
        }else {
            return "false";
        }
    }

    @RequestMapping(value = "/borrowInfo",produces = "text/html;charset=UTF-8")
    public String borrowInfo(){

        List<Map<String,Object>> mapList = new ArrayList<>();
        Map<String,Object> map;
        if (session.getAttribute("USER") != null) {
            User user = (User) session.getAttribute("USER");
            List<UserAction> userActionList = userActionService.findsUserAction(user.getId());

            for (UserAction userAction:userActionList) {
                Books books = booksService.findById(userAction.getBookid());
                map = new HashMap<>();
                map.put("id",userAction.getId());
                map.put("bookName",books.getName());
                map.put("type",books.getType());
                map.put("publishTime",books.getPublishedtime());
                map.put("returnTime",userAction.getReturntime());
                mapList.add(map);
            }
        }

        return JSON.toJSON(mapList).toString();
    }

    @RequestMapping(value = "/userName",produces = "text/html;charset=UTF-8")
    public String userName(){

        if (session.getAttribute("USER") != null) {
            User user = (User) session.getAttribute("USER");
            return JSON.toJSON(user).toString();
        }else {
            return "false";
        }
    }



}
